package com.project.servlet;

import com.project.bean.RoomBean;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("findAll")
public class FindAllServlet extends HttpServlet {
    @Autowired
    private IRoomService service;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应信息的MIME类型和编码集
        resp.setContentType("text/html;charset=utf-8");
        List<RoomBean> list = service.findAll();
        String str = "<table border='1' width='80%'>";
        str += "<thead><tr><th>地址</th><th>价格</th><th>房东</th></tr></thead>";
        str += "<tbody>";
        for (RoomBean roomBean : list) {
            str += "<tr><td>" + roomBean.getAddr() + "</td><td>" + roomBean.getMoney() +
                    "</td><td>" + roomBean.getLand() + "</td></tr>";
        }
        str += "</tbody></table>";
        resp.getWriter().print(str);
    }
}
